import Local_Window_TypeSelect from "@swda/Scene/SynthesisMenu/windowTypeSelect.js";
import { MenuBgParams as Params } from "@swda/configs/params";
import {
  Game_SynthesisItem,
  getExpectedRecipe,
  generateRecipe,
} from "./synthesisLogic.js";
// set parameters
const { bgBitmapSynthesis, bgBitmapSynthesis2 } = Params;

const setBgBitmap = (target, bitmapUrl) => {
  if (!bitmapUrl) return;
  const bitmap = ImageManager.loadBitmap("img/system/UI/", bitmapUrl);
  target._backgroundSprite.bitmap = bitmap;
};


//-------------------------------
// Scene_Synthesis 合成界面
//-------------------------------
Scene_Synthesis.prototype.initialize = function () {
  Scene_MenuBase.prototype.initialize.call(this);
  CP.PreLoadImageManager.preLoadImage("system/UI", bgBitmapSynthesis2, 0, true);
  this.clearRecipeItems();
  this._finalResult = null;
};

Scene_Synthesis.prototype.setLeftRecipeItem = function (item) {
  this._recipeItemLeft = item;
};

Scene_Synthesis.prototype.clearRecipeItems = function () {
  this._recipeItemLeft = null;
  this._recipeItemRight = null;
};

Scene_Synthesis.prototype.leftRecipeItem = function () {
  return this._recipeItemLeft;
};

Scene_Synthesis.prototype.setRightRecipeItem = function (item) {
  this._recipeItemRight = item;
};

Scene_Synthesis.prototype.rightRecipeItem = function () {
  return this._recipeItemRight;
};

Scene_Synthesis.prototype.create = function () {
  Scene_MenuBase.prototype.create.call(this);

  this.createAllWindows();
  this.setupHandlers();
};

Scene_Synthesis.prototype.createAllWindows = function () {
  this.createHelpWindow();
  this.createRecipeWindow();
  this.createTypeSelectWindow();
  this.createListWindow();
  this.createCommandWindow();
  this.createResultWindow();
};

Scene_Synthesis.prototype.createHelpWindow = function () {
  this._helpWindow = new Window_Help();
  this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height;
  this.setHelpWindowText("default");
  this.addWindow(this._helpWindow);
};

Scene_Synthesis.prototype.setHelpWindowText = function (symbol) {
  this.clearHelpWindowItem();
  switch (symbol) {
    case "default":
      this._helpWindow.setText("请选择一样炼妖物品。");
      break;
  }
};

Scene_Synthesis.prototype.clearHelpWindowItem = function () {
  this._helpWindow.setItem("");
};

Scene_Synthesis.prototype.setupHandlers = function () {
  this._typeWindow.setOnSelectMethod(this.onTypeSelect.bind(this));
  this._typeWindow.setHandler("ok", this.onTypeOk.bind(this));
  this._typeWindow.setHandler("cancel", this.onTypeCancel.bind(this));

  this._listWindow.setHandler("ok", this.onListOk.bind(this));
  //this._listWindow.setHandler("cancel", this.onListCancel.bind(this));
  this._listWindow.$on("select", this.updateItemPreviewWindow.bind(this));
  this._listWindow.$on("deselect", this.hideItemPreviewBox.bind(this));

  this._recipeWindow.setHandler("ok", this.onRecipeOk.bind(this));
  this._recipeWindow.setHandler("cancel", this.popScene.bind(this));

  // 预览窗口
  this._resultWindow.setHandler("synthesis", this.onResultOk.bind(this));
  this._resultWindow.setHandler("cancel", this.onResultCancel.bind(this));

  this._commandWindow.setHandler("select", this.onCommandSelect.bind(this));
  this._commandWindow.setHandler("result", this.onCommandResult.bind(this));
  this._commandWindow.setHandler("cancel", this.onCommandCancel.bind(this));
};

// 物品类型……………………………………………………………………………………………………………………………………………………………………
Scene_Synthesis.prototype.createTypeSelectWindow = function () {
  this._typeWindow = new Local_Window_TypeSelect(0, 0);
  const [x, y, width] = [650, 40, 500];
  this._typeWindow.x = x;
  this._typeWindow.y = y;
  this._typeWindow.width = width;
  this._typeWindow.opacity = 0;
  this._typeWindow.hide();
  this._typeWindow.deactivate();
  this.addWindow(this._typeWindow);
};

Scene_Synthesis.prototype.onTypeSelect = function () {
  this._listWindow.setItemType(this._typeWindow.index());
  this._listWindow.selectLast();
  this._listWindow.refresh();
};

Scene_Synthesis.prototype.onTypeOk = function () {
  this._listWindow.selectLast();
};

Scene_Synthesis.prototype.onTypeCancel = function () {
  this._typeWindow.deselect();
  this._listWindow.deselect();
  this._typeWindow.hide();
  this._listWindow.hide();
  this._commandWindow.hide();
  this._typeWindow.deactivate();
  this._listWindow.deactivate();
  this._recipeWindow.activate();
  this._recipeWindow.refresh();
  this.setHelpWindowText("default");
  this.showCommandWindow();
};
// 物品列表……………………………………………………………………………………………………………………………………………………………………
Scene_Synthesis.prototype.createListWindow = function () {
  const [x, y, width, height] = [680, this._typeWindow.y + 65, 482, 480];
  this._listWindow = new Window_SynthesisItemList(x, y, width, height, this);
  this._listWindow.setHelpWindow(this._helpWindow);
  this._listWindow.opacity = 0;
  this._listWindow.hide();
  this._listWindow.deactivate();
  this.addWindow(this._listWindow);
  this._listWindow._scrollbar.setOffset(5);
  this._typeWindow.setItemWindow(this._listWindow);
};

Scene_Synthesis.prototype.onListOk = function () {
  this.setCurrentItem(this._listWindow.item());
  $gameParty.setLastSSynthesisItem(this._listWindow.item());
  this._listWindow.deselect();
  this._typeWindow.deselect();
  this._listWindow.refresh();
  this._listWindow.deactivate();
  this._typeWindow.deactivate();
  this._recipeWindow.refresh();
  this._recipeWindow.activate();
  this._typeWindow.hide();
  this._listWindow.hide();
  this._commandWindow.refresh();
  this.setHelpWindowText("default");
  this.showCommandWindow();
};

Scene_Synthesis.prototype.showCommandWindow = function () {
  if (this._commandWindow.canShowResult()) {
    this._recipeWindow.deactivate();
    this._commandWindow.show();
    this._commandWindow.activate();
    this._commandWindow.select(1);
    this._helpWindow.hide();
  } else {
    this._commandWindow.hide();
  }
};

Scene_Synthesis.prototype.setCurrentItem = function (item) {
  if (this._recipeWindow.index() % 2 === 0) this.setLeftRecipeItem(item);
  else this.setRightRecipeItem(item);
};

Scene_Synthesis.prototype.setListPosition = function () {
  if (this._recipeWindow.index() % 2 === 0) {
    this._typeWindow.x = 650;
    this._listWindow.x = 680;
    this._listWindow._viewerWindow.x = 304;
    this._listWindow._viewerWindow.y = 108;
  } else {
    this._typeWindow.x = Graphics.boxWidth - 680 - 512 - 30 + 24; //this._listWindow.x - 30
    this._listWindow.x = Graphics.boxWidth - 680 - 512 + 24; //Graphics.boxWidth - x - 512
    this._listWindow._viewerWindow.x = 736;
    this._listWindow._viewerWindow.y = 108;
  }
};

Scene_Synthesis.prototype.updateItemPreviewWindow = function (id) {
  if (!this._listWindow) return;
  this._listWindow.showView();
};

Scene_Synthesis.prototype.hideItemPreviewBox = function () {
  if (!this._listWindow) return;
  this._listWindow.hideView();
};
// 指令窗口……………………………………………………………………………………………………………………………………………………………………
Scene_Synthesis.prototype.createCommandWindow = function () {
  const width = Graphics.boxWidth / 2;
  const x = (Graphics.boxWidth - width) / 2;
  const y = Graphics.boxHeight - this._listWindow.fittingHeight(1);
  this._commandWindow = new SWDA_Window_SynthesisCommand(x, y, width, this);
  this._commandWindow.hide();
  this.addWindow(this._commandWindow);
};

Scene_Synthesis.prototype.onCommandSelect = function () {
  this._commandWindow.deselect();
  this._commandWindow.deactivate();
  this._commandWindow.hide();
  this._recipeWindow.activate();
  this._helpWindow.show();
};

// 生成预览产物
Scene_Synthesis.prototype.onCommandResult = function () {
  const leftItem = new Game_SynthesisItem(this._recipeItemLeft);
  const rightItem = new Game_SynthesisItem(this._recipeItemRight);
  const recipe = getExpectedRecipe(leftItem, rightItem);// 预计产物
  const { produce } = recipe;

  setBgBitmap(this, bgBitmapSynthesis2);
  this._commandWindow.deactivate();
  this._commandWindow.hide();

  this._resultWindow.setFormulaInfo(recipe);
  this._resultWindow.setRecipeItems(
    this.leftRecipeItem(),
    this.rightRecipeItem()
  );
  this._resultWindow.setSynthesisResult(produce);
  this._resultWindow.refresh();
  this._resultWindow.activate();
  this._resultWindow.select(1);
  this._resultWindow.show();
  this._resultWindow.callUpdateHelp();
  this._helpWindow.show();
};

// 取消预览
Scene_Synthesis.prototype.onCommandCancel = function () {
  this._commandWindow.deselect();
  this._commandWindow.deactivate();
  this._commandWindow.hide();
  this._recipeWindow.activate();
  this._helpWindow.show();
};

// 预览结果
Scene_Synthesis.prototype.createResultWindow = function () {
  const x = 48;
  const y = 32;
  const width = Graphics.boxWidth - 96;
  const height = Graphics.boxHeight - this._helpWindow.height - 32;
  this._resultWindow = new SWDA_Window_SynthesisResult(x, y, width, height);
  this._resultWindow.setHelpWindow(this._helpWindow);
  this._resultWindow.opacity = 0;
  this._resultWindow.hide();
  this.addWindow(this._resultWindow);
};

// 点击炼化的回调——>实际进行生成(存在概率失败)
Scene_Synthesis.prototype.onResultOk = function () {
  const recipetFormulaInfo = this._resultWindow.getFormulaInfo();
  const recipetResult = generateRecipe(recipetFormulaInfo)

  const { isSuccess, finalProduce, expectedProduce, failureProduce, leftItem, rightItem } = recipetResult
  $gameParty.execRecipe(leftItem, rightItem, finalProduce);

  setBgBitmap(this, bgBitmapSynthesis);
  this.clearRecipeItems();

  this._helpWindow.hide();
  this._finalResult = [isSuccess, finalProduce, expectedProduce, !isSuccess && failureProduce];
  this._recipeWindow.requestSysAnimation(); // 生成动画
  this._resultWindow.refresh();

  this._resultWindow.deselect();
  this._resultWindow.deactivate();
  this._commandWindow.deselect();
  this._commandWindow.deactivate();
  this._commandWindow.refresh();
  this._listWindow.refresh();

  this._resultWindow.hide();

  return [isSuccess, finalProduce];
};

const _Swda_Scene_Synthesis_onResultOk = Scene_Synthesis.prototype.onResultOk;
Scene_Synthesis.prototype.onResultOk = function () {
  _Swda_Scene_Synthesis_onResultOk.call(this);
  this._resultWindow.clearSucessFlag();
};

Scene_Synthesis.prototype.showResultInfo = function (produce, failureProduce) {
  this.clearHelpWindowItem();
  if (!failureProduce)
    this._helpWindow.setText(
      CP.SynthesisManager.UI_SYNTHESIS_RESULT_TEXT.format(produce.name.split("（")[0])
    );
  else
    this._helpWindow.setText(
      CP.SynthesisManager.UI_SYNTHESIS_NOT_SUCCESS_TEXT.format(
        produce.name.split("（")[0],
        failureProduce.name.split("（")[0]
      )
    );
};

Scene_Synthesis.prototype.onResultCancel = function () {
  setBgBitmap(this, bgBitmapSynthesis);
  this._resultWindow.deselect();
  this._resultWindow.deactivate();
  this._resultWindow.hide();
  this._commandWindow.activate();
  this._commandWindow.show();
  this._helpWindow.hide();
};

// 物品栏……………………………………………………………………………………………………………………………………………………………………
Scene_Synthesis.prototype.createRecipeWindow = function () {
  const x = 0;
  const y = 0;
  const width = Graphics.boxWidth;
  const height = Graphics.boxHeight - this._helpWindow.height;
  this._recipeWindow = new Window_RecipeItem(x, y, width, height, this);
  this._recipeWindow.opacity = 0;
  this.addWindow(this._recipeWindow);
};

Scene_Synthesis.prototype.onRecipeOk = function () {
  this.setListPosition();
  this._recipeWindow.deactivate();
  this._recipeWindow.clearItemInfo(this._recipeWindow.index());
  this._typeWindow.show();
  this._listWindow.show();
  this._typeWindow.activate();
  this._listWindow.activate();
  this._typeWindow.selectLast();
};
